package com.zhanghu.hibernate.repository;

//import com.slyak.spring.jpa.GenericJpaRepository;
//import com.slyak.spring.jpa.TemplateQuery;

import com.zhanghu.hibernate.dto.SampleDTO;
import com.zhanghu.hibernate.dto.UserDTO;
import com.zhanghu.hibernate.dto.UserVO;
import com.zhanghu.hibernate.entity.Names;
import com.zhanghu.hibernate.entity.NamesOnly;
import com.zhanghu.hibernate.entity.NamesUser;
import com.zhanghu.hibernate.entity.ZkUser;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhanghu
 * @Description:
 * @Date: Create in 9:31 2020/1/7
 * @modified by:
 */
//@Repository
public interface ZkUserRepository extends JpaRepository<ZkUser, Integer>, JpaSpecificationExecutor<ZkUser> {
//    @TemplateQuery

    /**
     * 测试使用@Query注解进行动态查询并返回
     *
     * @param name 参数
     * @return map，这里为什么不返回实体类呢？因为使用原生sql进行查询，jpa通过接口的getXXX()方法进行映射
     */
    @Query(value = "select id as id, name as name from zk_user where if(:name != '', name=:name, 1=1)", nativeQuery = true)
    public List<Map<String, Object>> findDtos(@Param("name") String name);

    Collection<Names> findByName(String name);

    @Query("select u from ZkUser u")
    List<NamesOnly> findAlls();

    @Query("select u from ZkUser u")
    List<NamesUser> findAllss();

    @Query("select new com.zhanghu.hibernate.dto.UserDTO(u.username, u.name) from ZkUser u inner join ZUser z on u.id=z.id")
    List<UserDTO> findAlss();

    @Query("select new com.zhanghu.hibernate.dto.UserDTO(u.username, u.name) from ZkUser u inner join ZUser z on u.id=z.id")
    List<UserDTO> findAlss(Specification<ZkUser> specification);


    /**
     * 通过原生sql查询并返回自定义实体类
     *
     * @return
     */
    @Query(value = "select u.username as username, u.name as name from zk_user u", nativeQuery = true)
    List<UserVO> findAlNative();



    @Query("select u from ZkUser u where u.name=:#{#user.name}")
    List<ZkUser> get(@Param("user") ZkUser user);


//    ZkUser findAll(Integer id);
}
